using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Modelica.Annotations;

namespace DrawToolsLib {
    /// <summary>
    ///  Rectangle graphics object.
    /// </summary>
    public class GraphicsEllipse : GraphicsRectangleBase {
        private Ellipse src;   // Modelica Ellipse

        #region Constructors

        public GraphicsEllipse(Ellipse src) {
            if (src != null) {
                this.src = src;
            } else {
                this.src = new Ellipse();
            }
            this.graphicsActualScale = 1.0;
        }

        public GraphicsEllipse()
            : this(new Ellipse()) {
        }

        public GraphicsEllipse(double left, double top, double right, double bottom,
           double lineWidth, Color objectColor, Color fillColor, double actualScale, CoordinateSystem cs)
            : this() {
            this.ParentCS = cs;
            this.rectangleLeft = left;
            this.rectangleTop = top;
            this.rectangleRight = right;
            this.rectangleBottom = bottom;
            this.rectangleFillColor = fillColor;
            this.graphicsLineWidth = lineWidth;
            this.graphicsObjectColor = objectColor;
            this.graphicsActualScale = actualScale;
        }

        #endregion Constructors

        #region Overrides Modelica properties

        //{{x1, y1}, {x2, y2}}";
        protected override double rectangleLeft {
            get {
                return src.Extent.X1 - ex.X1;
            }
            set {
                src.Extent.X1 = value + ex.X1;
            }
        }

        protected override double rectangleTop {
            get {
                return ex.Y2 - src.Extent.Y1;
            }
            set {
                src.Extent.Y1 = ex.Y2 - value;
            }
        }

        protected override double rectangleRight {
            get {
                return src.Extent.X2 - ex.X1;
            }
            set {
                src.Extent.X2 = value + ex.X1;
            }
        }

        protected override double rectangleBottom {
            get {
                return ex.Y2 - src.Extent.Y2;
            }
            set {
                src.Extent.Y2 = ex.Y2 - value;
            }
        }

        protected override double graphicsLineWidth {
            get {
                return src.LineThickness;
            }
            set {
                src.LineThickness = value;
            }
        }
        protected override Color graphicsObjectColor {
            get {
                return Color.FromRgb(src.LineColor.R, src.LineColor.G, src.LineColor.B);
            }
            set {
                src.LineColor = value;
                RefreshDrawing();
            }
        }

        protected override Color rectangleFillColor {
            get {
                return Color.FromRgb(src.FillColor.R, src.FillColor.G, src.FillColor.B);
            }
            set {
                src.FillColor = value;
                RefreshDrawing();
            }
        }

        public override GraphicItem Source {
            get {
                return src;
            }
        }

        #endregion

        #region Overrides

        /// <summary>
        /// Draw object
        /// </summary>
        public override void Draw(DrawingContext drawingContext) {
            if (drawingContext == null) {
                throw new ArgumentNullException("drawingContext");
            }

            Rect r = Rectangle;

            Point center = new Point(
                (r.Left + r.Right) / 2.0,
                (r.Top + r.Bottom) / 2.0);

            double radiusX = (r.Right - r.Left) / 2.0;
            double radiusY = (r.Bottom - r.Top) / 2.0;

            drawingContext.DrawEllipse(
                new SolidColorBrush(rectangleFillColor),
                new Pen(new SolidColorBrush(ObjectColor), ActualLineWidth),
                center,
                radiusX,
                radiusY);

            base.Draw(drawingContext);
        }

        /// <summary>
        /// Test whether object contains point
        /// </summary>
        public override bool Contains(Point point) {
            if (IsSelected) {
                return this.Rectangle.Contains(point);
            } else {
                EllipseGeometry g = new EllipseGeometry(Rectangle);

                return g.FillContains(point) || g.StrokeContains(new Pen(Brushes.Black, ActualLineWidth), point);
            }
        }

        /// <summary>
        /// Test whether object intersects with rectangle
        /// </summary>
        public override bool IntersectsWith(Rect rectangle) {
            RectangleGeometry rg = new RectangleGeometry(rectangle);    // parameter
            EllipseGeometry eg = new EllipseGeometry(Rectangle);        // this object rectangle

            PathGeometry p = Geometry.Combine(rg, eg, GeometryCombineMode.Intersect, null);

            return (!p.IsEmpty());
        }

        public override object Clone() {
            GraphicsEllipse g = new GraphicsEllipse((Ellipse)this.src.Clone());
            g.selected = this.selected;
            g.ActualScale = this.ActualScale;
            return g;
        }

        #endregion Overrides

    }
}
